package com.ala4.oxcafe.boot.page;

import com.ala4.oxcafe.common.utils.collection.CollUtil;
import com.ala4.oxcafe.export.ExportResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author PING
 * @version 1.0.0
 * @date 2025/8/1 13:59
 */
@Data
public class PageResult<T> implements ExportResult<T>, Serializable {

    @Schema(description = "当前页", defaultValue = "1")
    private Integer pageNum = 1;

    @Schema(description = "每页数量", defaultValue = "10")
    private Integer pageSize = 20;

    @Schema(description = "总条数")
    private Integer total;

    @Schema(description = "总页数")
    private Integer pageCount;

    @Schema(description = "数据")
    private List<T> records;

    public static <T> PageResult<T> iPage(IPage<T> iPage) {
        PageResult<T> rspData = new PageResult<>();
        rspData.setRecords(CollUtil.isEmpty(iPage.getRecords()) ? CollUtil.newArrayList() : iPage.getRecords());
        rspData.setPageSize(Integer.valueOf(Long.toString(iPage.getSize())));
        rspData.setPageNum(Integer.valueOf(Long.toString(iPage.getCurrent())));
        rspData.setPageCount(Integer.valueOf(Long.toString(iPage.getPages())));
        rspData.setTotal(Integer.valueOf(Long.toString(iPage.getTotal())));
        return rspData;
    }

    @Override
    public Integer getTotalPage() {
        return this.pageCount;
    }

    @Override
    public Integer getDataTotal() {
        return this.total;
    }
}
